package com.cat.dataStructure03;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/minimum-penalty-for-a-shop/
 * @create 2025/11/7 18:11
 * @since JDK17
 */

public class Solution15 {
    public int bestClosingTime(String customers) {
        int n = customers.length(), ans = Integer.MAX_VALUE, idx = 0;
        char[] arr = customers.toCharArray();
        int[] suf = new int[n + 1], pre = new int[n + 1];
        for (int i = n - 1; i >= 0; i--) {
            suf[i] = suf[i + 1] + (arr[i] == 'Y' ? 1 : 0);
        }
        for (int i = 0; i < n; i++) {
            pre[i + 1] = pre[i] + (arr[i] == 'N' ? 1 : 0);
        }
        for (int i = 0; i <= n; i++) {
            if (ans > pre[i] + suf[i]) {
                ans = pre[i] + suf[i];
                idx = i;
            }
        }

        return idx;
    }
}
